// 导入正则校验函数
let listMid = (req, res, next) => {
  // 接受name,age参数并进行校验
  let { current, pagesize, search, filter, saleType, sortType, sortMethod, category } = req.query;

  // 正则校验
  if (current && !/^[1-9]\d*$/.test(current)) return next({ code: 3, message: '参数current格式错误' });
  if (pagesize && !/^[1-9]\d*$/.test(pagesize)) return next({ code: 3, message: '参数pagesize格式错误' });
  if (filter && !/^(hot|sale)$/.test(filter)) return next({ code: 3, message: '参数filter格式错误' });
  if (saleType && !/^(5|6|7|8|9|10)$/.test(saleType)) return next({ code: 3, message: '参数saleType格式错误' });
  if (sortType && !/^(id|sale|price)$/.test(sortType)) return next({ code: 3, message: '参数sortType格式错误' });
  if (sortMethod && !/^(ASC|DESC)$/.test(sortMethod)) return next({ code: 3, message: '参数sortMethod格式错误' });
  next();
}

let detailMid = (req, res, next) => {
  let { id } = req.query;
  if (!id) return next({ code: 2, message: '参数id必须传递' });
  next();
}

let findById = async (id) => {
  return model.findById(id)
}

module.exports = {
  listMid, detailMid,findById
}